/**
 * 				ServerRuntime.java
 *				------------
 *		
 *   Author:	bin04017@gmail.com
 *	 Created:	13.12.2006
 *	 Project:	MP3-Sync (Mochito)
 *	 Package:	serverClasses
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */
package mochito.serverClasses;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * @author  bin04017
 */
public class ServerRuntime implements Runnable {

	private String host = "localhost";
	private ServerRMI serverRMI;
	
	public ServerRuntime(){
		
	}
	public ServerRuntime(String host) throws RemoteException{
		this.host = host;
		this.serverRMI  = new ServerRMI();
	}
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		try {
			LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
		}

		catch (RemoteException ex) {
			System.out.print(ex);
		}
		try {
			System.out.println("Starting Server: rmi://"+host+"/MP3");
			Naming
					.bind("rmi://"+host+"/MP3",
							serverRMI);
			System.out.println("Done");
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("Restarting running server");
			try {
				Naming.unbind("rmi://"+host+"/MP3");
				Naming.rebind("rmi://"+host+"/MP3",
						serverRMI);
				System.out.println("Done");
			} catch (RemoteException e) {
				System.out.print(ex);
			} catch (MalformedURLException e) {
				System.out.print(ex);
			} catch (NotBoundException e) {
				System.out.print(ex);
			}
		} 

	}
	/**
	 * @return  the host
	 */
	public String getHost() {
		return host;
	}
	/**
	 * @return  the serverRMI
	 */
	public ServerRMI getServerRMI() {
		return serverRMI;
	}

}
